package com.zhida.smartcontrol.dialog.addqddialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jay.tools.utils.EventBusUtils;
import com.jay.tools.utils.SoftInputUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/16 016 16:50
 * description: 添加区段
 */
public class AddQdDialogFragment extends MVPBaseDialogFragment<AddQdDialogContract.View, AddQdDialogPresenter> implements AddQdDialogContract.View {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.et_qd)
    EditText mEtQd;

    private String mId;
    private String mProjectBaseId;
    private String mName;

    public static AddQdDialogFragment newInstance(String id, String projectBaseId, String name) {
        Bundle args = new Bundle();
        args.putString("id", id);
        args.putString("pId", projectBaseId);
        args.putString("name", name);
        AddQdDialogFragment fragment = new AddQdDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        mCanceledOnTouchOutside = true;
        return R.layout.dialog_add_qd;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mName = bundle.getString("name");
        mId = bundle.getString("id");
        mProjectBaseId = bundle.getString("pId");
        if (TextUtils.isEmpty(mId)) {
            mTvTitle.setText("添加区段");
            mEtQd.setBackgroundResource(R.drawable.s_bg_2);
        } else {
            mTvTitle.setText("编辑区段");
            mEtQd.setBackgroundResource(R.drawable.s_bg_3);
            mEtQd.setText(mName);
        }
    }

    @OnClick(R.id.btn_confirm)
    public void click(View v) {
        SoftInputUtils.hideSoftInput(mActivity, mEtQd);
        String name = mEtQd.getText().toString().trim();
        if (TextUtils.isEmpty(mId)) {
            mPresenter.addGroup(name, mProjectBaseId);
        } else {
            if (mName.equals(name)) {
                dismiss();
                return;
            }
            mPresenter.editGroup(name, mId, mProjectBaseId);
        }
    }

    @Override
    public void onSuccess() {
        EventBusUtils.post(new AddGroupEvent());
        dismiss();
    }

}
